PROJECTENGRAM

JAVA-OOP-MOCK-ANSWERS

JAVA-OOP-Mock-Answers

Tags: #◇CCPROG3 #◇Object-Oriented Concepts #◇Programming #◇Computer Science #◇Java #◇Object-Oriented Programming #◇Software Engineering Principles #◇Algorithms


◇Overview

  1. C. Object-Oriented Programming

◇Details / Theory

◇Part I: Multiple Choice (Theory & Concepts)

  1. C. Object-Oriented Programming
    Explanation: OOP focuses on objects (entities/nouns) that encapsulate data and behavior, whereas Procedural focuses on functions (actions/verbs).

  2. B. Stack
    Explanation: Primitive types (int, double, etc.) are stored directly in the Stack memory. Objects are stored in the Heap.

  3. C. null
    Explanation: The default value for any object reference (String, Arrays, Classes) is null.

  4. C. final
    Explanation: The final keyword makes a variable constant, a method non-overridable, or a class non-inheritable.

  5. C. It can be called without creating an object of the class.
    Explanation: Static members belong to the class itself, not any specific instance.

  6. C. Encapsulation
    Explanation: Encapsulation is about bundling data and methods and restricting access (hiding details) via access modifiers.

  7. B. private
    Explanation: Fields should be private to enforce encapsulation, accessible only via public getters/setters.

  8. D. It has no return type
    Explanation: Constructors look like methods but have NO return type, not even void.

  9. C. Aggregation
    Explanation: Aggregation is a "Has-a" relationship where the child can exist independently (Weak coupling).

  10. B. Composition
    Explanation: Composition is a strong "Part-of" relationship where the child dies if the parent dies.

  11. C. Set
    Explanation: The Set interface (e.g., HashSet) strictly prohibits duplicate elements.

  12. B. LinkedList
    Explanation: LinkedList is faster for insertion/deletion (O(1)O(1)) compared to ArrayList (O(n)O(n)) because it doesn't need to shift elements.

  13. C. Autoboxing
    Explanation: Java automatically converts primitives to their wrapper classes (e.g., int to Integer).

  14. C. extends
    Explanation: The keyword extends is used for class inheritance.

  15. A. One
    Explanation: Java supports Single Inheritance only for classes. A class can extend only one parent.

  16. B. Calls the superclass's constructor.
    Explanation: super() invokes the constructor of the parent class and must be the first line in the child constructor.

  17. C. Compile-time Polymorphism
    Explanation: Method Overloading is resolved at compile-time (static binding).

  18. C. equals()
    Explanation: == compares memory addresses. equals() is used to compare the actual content/values of objects.

  19. C. It causes a Compilation Error.
    Explanation: Abstract classes cannot be instantiated directly using new.

  20. C. A class can implement multiple interfaces.
    Explanation: While classes allow single inheritance, they can implement multiple interfaces.

  21. B. implements
    Explanation: The keyword implements is used to use an Interface.

  22. C. Throwable
    Explanation: Throwable is the superclass of both Error and Exception.

  23. C. finally
    Explanation: The finally block executes always, regardless of whether an exception was thrown or caught.

  24. C. IOException
    Explanation: IOException is a Checked Exception (must be handled). The others are RuntimeExceptions (Unchecked).

  25. C. void method() throws Exception
    Explanation: The throws keyword in the method signature declares that the method might throw an exception.

  26. C. Controller
    Explanation: In MVC, the Controller handles user input and updates the Model.

  27. C. JButton
    Explanation: JButton is the Swing class for a push button.

  28. B. Returns a random double between 0.0 and 1.0.
    Explanation: Math.random() returns a double value greater than or equal to 0.0 and less than 1.0.

  29. D. private
    Explanation: private members are accessible only within the class they are defined in.

  30. B. Linking a method call to the method body at runtime based on the object type.
    Explanation: This is the mechanism behind Method Overriding (Runtime Polymorphism).


◇Part II: UML Analysis

  1. B. Private
    Explanation: The minus sign (-) represents private visibility.

  2. A. Public
    Explanation: The plus sign (+) represents public visibility.

  3. C. Composition
    Explanation: A filled/solid diamond represents Composition (Strong relationship).

  4. B. They are also destroyed.
    Explanation: In Composition, the lifecycle of the part is dependent on the whole.

  5. B. Aggregation
    Explanation: An empty/hollow diamond represents Aggregation (Weak relationship).

  6. B. They continue to exist independently.
    Explanation: In Aggregation, the parts can survive without the container.

  7. A. Vehicle
    Explanation: The arrow points towards the parent (Superclass).

  8. C. class Car extends Vehicle
    Explanation: Solid line with closed arrow means Inheritance (extends).

  9. B. It cannot be instantiated and has abstract methods.
    Explanation: Interfaces are abstract contracts.

  10. B. class Guitar implements Playable
    Explanation: Dashed line with closed arrow means Implementation (implements).

  11. C. Zero to many.
    Explanation: 0..* means zero or more (many).

  12. A. Exactly one.
    Explanation: "1" means exactly one.

  13. C. Protected
    Explanation: The # symbol stands for protected visibility.

  14. B. Static method
    Explanation: Underlined members in UML denote static.

  15. A. Abstract class
    Explanation: Italicized names denote abstract classes or methods.


◇Part III: Code Analysis

  1. B. 1 2
    Explanation: id is instance-specific (1, then 2). count is static (shared), so it increments to 2 and stays 2 for everyone.

  2. C. false true
    Explanation: s1 == s2 is false (different memory addresses). s1.equals(s2) is true (same content "Java").

  3. B. B
    Explanation: Polymorphism. The object is of type B, so B's overridden show() method is called.

  4. D. P C
    Explanation: Constructor chaining. Child constructor implicitly calls super() (Parent constructor) first.

  5. C. Error Done
    Explanation: Index 3 is out of bounds (0, 1, 2 exist). Exception is caught ("Error"), then finally executes (" Done").

  6. B. [A, C, B]
    Explanation: add(1, "C") inserts "C" at index 1, shifting "B" to the right.

  7. C. Compilation Error
    Explanation: Shape is abstract and cannot be instantiated.

  8. A. IntDouble
    Explanation: Method overloading selects the method matching the parameter type (int vs double).

  9. C. Caught Next
    Explanation: Exception is thrown, caught ("Caught "), and then program flow continues ("Next").

  10. B. Compilation Error
    Explanation: Interface variables are implicitly final (constants) and cannot be reassigned.

  11. C. 9
    Explanation: Casting double to int truncates the decimal part (does not round).

  12. B. false
    Explanation: true && false is false.

  13. B. Java
    Explanation: Strings are immutable. s.concat() returns a new string, but s itself remains "Java" unless reassigned.

  14. B. 1
    Explanation: 10 % 3 is the remainder of 10 divided by 3 (3 * 3 = 9, remainder 1).

  15. A. 012
    Explanation: Loop runs for i = 0, 1, 2. Stops when i = 3.

  16. B. One Two
    Explanation: Case 1 matches. There is no break after Case 1, so it "falls through" and executes Case 2 as well.

  17. B. false
    Explanation: The default equals() implementation in Object compares memory addresses (like ==), which are different for two new objects.

  18. B. 2
    Explanation: Integer division (int / int) results in an int. The decimal .5 is truncated.

  19. A. 5 6
    Explanation: x++ (post-increment) prints the current value (5) then increments it. Next print shows 6.

  20. C. ArrayIndexOutOfBoundsException
    Explanation: Array size is 5, so valid indices are 0-4. Index 5 is out of bounds.

  21. B. 20
    Explanation: this.x = x correctly assigns the parameter 20 to the instance variable x.

  22. A. 10
    Explanation: super.x accesses the parent's variable x (10), ignoring the child's shadowed x (20).

  23. B. Null
    Explanation: Calling a method on null throws NullPointerException, which is caught.

  24. C. 3
    Explanation: arr[1] selects the second array {3, 4}. [0] selects the first element of that array, which is 3.

  25. C. Compilation Error
    Explanation: final variables cannot be reassigned.


◇Common Pitfalls

Checking only the letter answer without reading the explanation — understanding why is crucial for exam variants.
Memorizing code output without understanding the underlying mechanism (e.g., dynamic dispatch, String immutability).
Ignoring the UML analysis rationales — UML notation rules (visibility, line styles, multiplicity) are commonly tested.

◇Summary

TL;DR — Complete 70-item answer key with explanations covering OOP theory, UML notation rules, and code tracing mechanics (static sharing, polymorphic dispatch, constructor chaining, exception flow, autoboxing).

Related:
Source / Reference:


Made by 2ru17

Please contact me through discord or my the university email for any problems regarding the reviewer. Thanks!

◇References & Navigation